#ifndef _ICODECELL_H_
#define _ICODECELL_H_

#include "member_BFont.h"

namespace BWE
{
	class CodeCell
	{
	public:
		inline CodeCell()
		{
			code = 0;
			image = 0;
			image_left = 0;
			image_top = 0;
		}
		inline CodeCell(int x, int y, int w, int h)
		{
			rect.set(x, y, w, h);
			code = 0;
			image = 0;
			image_left = 0;
			image_top = 0;
		}
		inline CodeCell(const CodeCell& other)
		{
			code = other.code;
			rect = other.rect;
			image = other.image;
			image_left = other.image_left;
			image_top = other.image_top;
			image_rect = other.image_rect;
		}

		inline void fresh(const BFont* font, int rowHeight = 0)
		{
			if (font)
			{
				BSize size = font->codeSize(code);
				rect.setSize(size.width(), rowHeight);

				CodeInfo* info = font_member(font)->getCodeInfo(code);
				if (info && info->bitmap)
				{
					const BImage* img = info->bitmap;
					image = img;
					image_left = info->left;
					image_top = info->top;
					image_rect.setSize(img->width(), img->height());
				}
			}
			else
			{
				rect.setSize(3, rowHeight);
				image = 0;
				image_left = 0;
				image_top = 0;
				image_rect.setSize(0, 0);
			}
		}
		inline bool isSpace() const
		{
			return code == ' '
				|| code == '\t'
				|| code == '\n'
				|| code == '\r'
				|| code == '\v';
		}

		inline bool operator == (BCode code)
		{
			return this->code == code;
		}
		inline bool operator != (BCode code)
		{
			return this->code != code;
		}

		BCode			code;
		BRect			rect;
		int				image_left;
		int				image_top;
		BRect			image_rect;
		const BImage*	image;
	};

	class ITextBlock
	{
	public:
		ITextBlock();
		ITextBlock(const ITextBlock& other);
		~ITextBlock();

		void reset(const BString& text);
		void append(const BString& text);
		bool insert(int pos, BCode code);
		bool insert(int pos, const BString& text);
		void refresh(BFont* font);
		void adjust();

		int wordWidth(int index) const;

		int tail() const;

		bool				dirty;
		int					line;
		BString				text;
		int					width;
		int					rowHeight;
		BFontHolder			font;
		BRect				rect;
		BArray<CodeCell>	cells;
	};

}

#endif